package com.jdcjk.util;

import android.text.TextUtils;

import com.jdcjk.project.main.model.entity.CarListInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2015/4/24.
 * 搜索对话框工具
 */
public class SearchEditTextUtil {

    public static ArrayList<CarListInfo> search(final String str,
                                                final List<CarListInfo> allCarListInfo) {
        ArrayList<CarListInfo> contactList = new ArrayList<>();
        // 搜索
        if (!TextUtils.isEmpty(str)) {
            for (CarListInfo carListInfo : allCarListInfo) {
                if (carListInfo.getDisplayid() != null && carListInfo.getCarId() != null) {
                    if (carListInfo.getDisplayid().contains(str) || carListInfo.getCarId().contains(str)) {
                        contactList.add(carListInfo);
                    }
                }
            }
            return contactList;
        }

        //判断是搜索词中是否为中文
        try {
            if (str.getBytes("GBK").length == str.length() * 2) {
                for (CarListInfo contact : allCarListInfo) {
                    if (contact.getCarId().contains(str)) {
                        contactList.add(contact);
                    }
                }
            } else {
                final String result = CharacterParser.getInstance().getSelling(str);
                for (CarListInfo carListInfo : allCarListInfo) {
                    if (contains(carListInfo, result)) {
                        contactList.add(carListInfo);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return contactList;
    }

    public static boolean contains(CarListInfo carListInfo, String search) {
        if (TextUtils.isEmpty(carListInfo.getTerminalNumber()) || TextUtils.isEmpty(search)) {
            return false;
        }

        boolean flag = false;

        // 简拼匹配,如果输入在字符串长度大于6就不按首字母匹配了
        if (search.length() < 6) {
            // String firstLetters = FirstLetterUtil.getFirstLetter(contact
            // .getName());
            // 获得首字母字符串
            String firstLetters = UnicodeGBK2Alpha
                    .getSimpleCharsOfString(carListInfo.getTerminalNumber());
            // String firstLetters =
            // PinYinUtil.getFirstSpell(contact.getName());
            // 不区分大小写
            Pattern firstLetterMatcher = Pattern.compile("^" + search,
                    Pattern.CASE_INSENSITIVE);
            flag = firstLetterMatcher.matcher(firstLetters).find();
        }

        if (!flag) { // 如果简拼已经找到了，就不使用全拼了
            // 全拼匹配
            // ChineseSpelling finder = ChineseSpelling.getInstance();
            // finder.setResource(contact.getName());
            // 不区分大小写
            Pattern pattern2 = Pattern
                    .compile(search, Pattern.CASE_INSENSITIVE);
            Matcher matcher2 = pattern2.matcher(CharacterParser.getInstance().getSelling(carListInfo
                    .getTerminalNumber()));
            flag = matcher2.find();
        }

        return flag;
    }
}
